package sapling.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sapling.dao.LogDao;
import sapling.dao.OutboundOrderDao;
import sapling.dao.TreeDao;
import sapling.model.Log;
import sapling.model.OutboundOrder;
import sapling.model.Tree;
import sapling.service.TreeService;
import sapling.utils.ExcelUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by ever on 2016/10/9.
 */
@Controller
@RequestMapping(value = "/admin/log")
public class LogAction {
    @Autowired
    private LogDao logDao;
    @Autowired
    private TreeService treeService;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    OutboundOrderDao outboundOrderDao;

    @RequestMapping("/examine")
    public String list(Model model) {
        model.addAttribute("log", logDao.findAll(new Sort(Sort.Direction.DESC, "operationtime")));
        List<OutboundOrder> outStatus = outboundOrderDao.findByStatus(1);
        List<Tree> treeList = new ArrayList<>();
        for (OutboundOrder out : outStatus) {
            if (Objects.nonNull(out.getTrees()) && out.getTrees().size() > 0) {
                Tree tree = out.getTrees().get(0);
                tree.setSerialNumber(out.getGmt_payment() + "");
                tree.setNumber(Integer.parseInt(out.getQuantity() + ""));
                if (Objects.nonNull(out.getTransactionAmount())) {
                    tree.setPrice(out.getTransactionAmount().doubleValue());
                } else {
                    tree.setPrice(0);
                }
                tree.setUsername(out.getName());
                treeList.add(tree);
            }
        }
        model.addAttribute("treesout", treeList);
        model.addAttribute("treesput", treeDao.findPutWarehouse());
        return "admin/log/log_list";
    }

    @RequestMapping("/delete")
    public String delete(Long log_id) {
        if (Objects.nonNull(log_id)) {
            logDao.delete(log_id);
        }
        return "redirect:/admin/log/examine";
    }

    @RequestMapping("/export")
    @ResponseBody
    public String export(int index, String time)  {
        String info = "error";
        if (Objects.nonNull(time) && time.length() > 0) {
            String str[] = time.split(" - ");
            if (str.length == 2) {
                long lTime1=0;
                long lTime2=0;
                Date dt1 =new Date();
                Date dt2 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                   dt1 = sdf.parse(str[0]+" 00:00:00");
                   dt2 = sdf.parse(str[1]+" 00:00:00");
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(dt2);
                    cal.add(Calendar.DATE, 1);
                   lTime1=dt1.getTime()/1000;
                   lTime2=cal.getTime().getTime()/1000;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (index == 0) {
                    List<Log> logs = logDao.findSE(str[0], str[1]);
                    if (Objects.nonNull(logs) && logs.size() > 0) {
                        info = ExcelUtil.exportLog(logs);
                    }
                } else if (index == 1) {
                    List<Tree> trees = treeDao.findPutSE(str[0]+" 00:00:00", str[1]+" 23:59:59");
                    if (Objects.nonNull(trees) && trees.size() > 0) {
                        info = ExcelUtil.exportOPut(trees, "产地", "入库时间");
                    }
                } else if (index == 2) {
                    List<OutboundOrder> outStatus = outboundOrderDao.findByStatus(1);
                    List<Tree> treeList = new ArrayList<>();
                    for (OutboundOrder out : outStatus) {
                        if (Objects.nonNull(out.getTrees()) && out.getTrees().size() > 0) {
                            Tree tree = out.getTrees().get(0);
                            tree.setSerialNumber(out.getGmt_payment() + "");
                            tree.setNumber(Integer.parseInt(out.getQuantity() + ""));
                            if (Objects.nonNull(out.getTransactionAmount())) {
                                tree.setPrice(out.getTransactionAmount().doubleValue());
                            } else {
                                tree.setPrice(0);
                            }
                            tree.setUsername(out.getName());
                            long t=tree.getOutWarehouseTime().getTime()/1000;
                            if((lTime1<=t)&&(lTime2>=t)){
                                treeList.add(tree);
                            }
                        }
                    }
                    if (Objects.nonNull(treeList) && treeList.size() > 0) {
                        info = ExcelUtil.exportOPut(treeList, "去向", "出库时间");
                    }
                }
            }
        }
        return info;
    }
}
